package com.blockchain.securewallet.model.db.convert;

import com.blockchain.securewallet.model.db.CoinWallet;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.greendao.converter.PropertyConverter;

import java.util.List;

/**
 * Created by Chain-Cloud on 2018/6/8.
 */

public class CoinWalletConverter implements PropertyConverter<List<CoinWallet>, String> {

    private Gson gson;

    @Override
    public List<CoinWallet> convertToEntityProperty(String databaseValue) {
        if (databaseValue == null) {
            return null;
        }
        else {
            if (gson == null){
                gson = new Gson();
            }
            List<CoinWallet> coinWallets = gson.fromJson(databaseValue,new TypeToken<List<CoinWallet>>(){}.getType());
            return coinWallets;
        }
    }

    @Override
    public String convertToDatabaseValue(List<CoinWallet> entityProperty) {
        if(entityProperty==null){
            return null;
        } else{
            if (gson == null){
                gson = new Gson();
            }
            return gson.toJson(entityProperty);
        }
    }
}
